home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 018 / amigadisplay / beep.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  3KB  |  91 lines

  1. /***************************************\
  2. * This file controls the audio (beep.c) *
  3. \***************************************/
  4.  
  5. #include "exec/types.h"
  6. #include "exec/memory.h"
  7. #include "devices/audio.h"
  8.  
  9. extern struct MsgPort *CreatePort();
  10.  
  11. /* allocation map: first try for a left chan, else right */
  12. static UBYTE allocationMap[] = {1, 8, 2, 4};
  13.  
  14. #define REPS    3          /* number of copies of pattern in buffer */
  15. #define BUFSIZE (32*REPS)
  16.  
  17. static struct IOAudio *ioa = NULL;
  18.  
  19. InitAudio(loudness) int loudness; {
  20.    int i;
  21.    ioa = (struct IOAudio *)AllocMem(sizeof(*ioa), MEMF_PUBLIC|MEMF_CLEAR);
  22.    if (ioa == NULL) return(TRUE);
  23.    ioa->ioa_Request.io_Message.mn_Node.ln_Pri = 10;
  24.    ioa->ioa_Request.io_Message.mn_ReplyPort = CreatePort("Beeper", 0);
  25.    if (ioa->ioa_Request.io_Message.mn_ReplyPort == NULL) return(Flush(FALSE));
  26.    ioa->ioa_Data = allocationMap;
  27.    ioa->ioa_Length = sizeof(allocationMap);
  28.    if (OpenDevice(AUDIONAME, 0, ioa, 0)) return(Flush(FALSE));
  29.    ioa->ioa_Request.io_Command = CMD_WRITE;
  30.    ioa->ioa_Request.io_Flags = ADIOF_PERVOL;
  31.    ioa->ioa_Data = (UBYTE *)AllocMem(BUFSIZE, MEMF_CHIP);
  32.    if (ioa->ioa_Data == NULL) return(Flush(TRUE));
  33.    for (i=0; i<BUFSIZE; i++) {
  34.       int j;
  35.       j = Sine32nd(i*2) * 4 + Sine32nd(i*3) * 9;
  36.       ioa->ioa_Data[i] = (j > 0 ? j+65 : j-65) / 130;
  37.       }
  38.    ioa->ioa_Length = BUFSIZE;
  39.    /* start a request so we can wait for it later (in Flush) */
  40.    ioa->ioa_Period = ioa->ioa_Cycles = 200;
  41.    ioa->ioa_Volume = 0;
  42.    BeginIO(ioa);
  43.    SetBeeper(loudness);
  44.    return(FALSE);
  45.    }
  46.  
  47. SetBeeper(loudness) int loudness; {
  48.    if (loudness < 0 || loudness > 4) ioa->ioa_Volume = 0;
  49.    else {
  50.       ioa->ioa_Period = 127*(loudness+2)/2;
  51.       ioa->ioa_Cycles = 480/(loudness+2);
  52.       ioa->ioa_Volume = (60 >> loudness) + 4;
  53.       }
  54.    }
  55.  
  56. Beep() {
  57.    if ((! ioa) || ioa->ioa_Volume == 0) return(TRUE); /* need video beep */
  58.    BeginIO(ioa);
  59.    return(FALSE);
  60.    }
  61.  
  62. CleanUpBeeper() {
  63.    if (ioa) Flush(TRUE);
  64.    }
  65.  
  66. static Flush(opened) int opened; {
  67.    if (opened) {
  68.       WaitIO(ioa);
  69.       if (ioa->ioa_Data) FreeMem(ioa->ioa_Data, BUFSIZE);
  70.       CloseDevice(ioa);
  71.       }
  72.    if (ioa->ioa_Request.io_Message.mn_ReplyPort)
  73.       DeletePort(ioa->ioa_Request.io_Message.mn_ReplyPort);
  74.    FreeMem(ioa, sizeof(*ioa));
  75.    ioa = NULL;
  76.    return(TRUE);
  77.    }
  78.  
  79. static Sine32nd(n) int n; { /* returns 1270*sin(2PI*n/32), rounded */
  80.    static int qtr[] = {0, 248, 486, 706, 898, 1056, 1173, 1246, 1270};
  81.    if (n < 0) return(-Sine32nd(-n));
  82.    n %= 32;
  83.    switch (n/8) {
  84.       case 0: return(qtr[n]);
  85.       case 1: {n = 16-n; return(qtr[n]);}
  86.       case 2: {n -= 16; return(-qtr[n]);}
  87.       case 3: {n = 32-n; return(-qtr[n]);}
  88.       }
  89.    return(0);
  90.    }
  91.